home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / inventor / SpaceballViewer / MyColPatch.c++ < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  5.2 KB  |  198 lines

  1. /*
  2.  * Copyright (c) 1990-94 Silicon Graphics, Inc.
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and
  5.  * its documentation for any purpose is hereby granted without fee, provided
  6.  * that the name of Silicon Graphics may not be used in any advertising or
  7.  * publicity relating to the software without the specific, prior written
  8.  * permission of Silicon Graphics.
  9.  *
  10.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  11.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  12.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  13.  *
  14.  * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
  15.  * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
  16.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE
  17.  * POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN
  18.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  19.  */
  20. /*
  21.  * Copyright (C) 1990-93   Silicon Graphics, Inc.
  22.  *
  23.  _______________________________________________________________________
  24.  ______________  S I L I C O N   G R A P H I C S   I N C .  ____________
  25.  |
  26.  |   $Revision: 1.1013 $
  27.  |
  28.  |   Classes:
  29.  |    MyColorPatch
  30.  |
  31.  |   Author(s)    : Alain Dumesny
  32.  |
  33.  ______________  S I L I C O N   G R A P H I C S   I N C .  ____________
  34.  _______________________________________________________________________
  35.  */
  36.  
  37. #if DEBUG
  38. #include <stream.h>
  39. #endif
  40.  
  41. #include "MyUIRegion.h"
  42. #include "MyColorPatch.h"
  43.  
  44. #include <GL/gl.h>
  45.  
  46.  
  47. /*
  48.  * Defines
  49.  */
  50.  
  51. #define SIDE    (UI_THICK + 2 + UI_THICK)
  52.  
  53. ////////////////////////////////////////////////////////////////////////
  54. //
  55. // Public constructor - build the widget right now
  56. //
  57. MyColorPatch::MyColorPatch(
  58.     Widget parent,
  59.     const char *name, 
  60.     SbBool buildInsideParent)
  61.     : SoXtGLWidget(
  62.         parent,
  63.         name, 
  64.         buildInsideParent, 
  65.         SO_GLX_RGB, 
  66.         FALSE) // tell GLWidget not to build just yet  
  67. //
  68. ////////////////////////////////////////////////////////////////////////
  69. {
  70.     // In this case, this component is what the app wants, so buildNow = TRUE
  71.     constructorCommon(TRUE);
  72. }
  73.  
  74. ////////////////////////////////////////////////////////////////////////
  75. //
  76. // SoEXTENDER constructor - the subclass tells us whether to build or not
  77. //
  78. MyColorPatch::MyColorPatch(
  79.     Widget parent,
  80.     const char *name, 
  81.     SbBool buildInsideParent, 
  82.     SbBool buildNow)
  83.     : SoXtGLWidget(
  84.         parent,
  85.         name, 
  86.         buildInsideParent, 
  87.         SO_GLX_RGB, 
  88.         FALSE) // tell GLWidget not to build just yet  
  89. //
  90. ////////////////////////////////////////////////////////////////////////
  91. {
  92.     // In this case, this component may be what the app wants, 
  93.     // or it may want a subclass of this component. Pass along buildNow
  94.     // as it was passed to us.
  95.     constructorCommon(buildNow);
  96. }
  97.  
  98. ////////////////////////////////////////////////////////////////////////
  99. //
  100. // Called by the constructors
  101. //
  102. // private
  103. //
  104. void
  105. MyColorPatch::constructorCommon(SbBool buildNow)
  106. //
  107. //////////////////////////////////////////////////////////////////////
  108. {
  109.     // init local vars
  110.     color[0] = color[1] = color[2] = 0;
  111.     setGlxSize( SbVec2s(40, 40) );  // default size
  112.     
  113.     // Build the widget tree, and let SoXtComponent know about our base widget.
  114.     if (buildNow) {
  115.     Widget w = buildWidget(getParentWidget());
  116.     setBaseWidget(w);
  117.     }
  118. }
  119.  
  120. ////////////////////////////////////////////////////////////////////////
  121. //
  122. //    Dummy virtual destructor.
  123. //
  124. MyColorPatch::~MyColorPatch()
  125. //
  126. ////////////////////////////////////////////////////////////////////////
  127. {
  128. }
  129.  
  130. ////////////////////////////////////////////////////////////////////////
  131. //
  132. //  Routine which draws the color patch
  133. //
  134. // Use: virtual public
  135.  
  136. void
  137. MyColorPatch::redraw()
  138. //
  139. ////////////////////////////////////////////////////////////////////////
  140. {
  141.     if (! isVisible())
  142.     return;
  143.     
  144.     glXMakeCurrent(getDisplay(), getNormalWindow(), getNormalContext());
  145.     
  146.     // draw border
  147.     SbVec2s size = getGlxSize();
  148.     drawDownUIRegion(0, 0, size[0]-1, size[1]-1);
  149.     
  150.     // draw the patch color
  151.     glColor3fv(color.getValue());
  152.     glRecti(SIDE, SIDE, size[0] - SIDE, size[1] - SIDE);
  153. }
  154.  
  155.  
  156. ////////////////////////////////////////////////////////////////////////
  157. //
  158. //  Sets the patch color
  159. //
  160. // Use: public
  161.  
  162. void
  163. MyColorPatch::setColor(const SbColor &rgb)
  164. //
  165. ////////////////////////////////////////////////////////////////////////
  166. {
  167.     // save color
  168.     color = rgb;
  169.     
  170.     // now show the color change
  171.     if (! isVisible())
  172.     return;
  173.     glXMakeCurrent(getDisplay(), getNormalWindow(), getNormalContext());
  174.     
  175.     glColor3fv(color.getValue());
  176.     SbVec2s size = getGlxSize();
  177.     glRecti(SIDE, SIDE, size[0] - SIDE, size[1] - SIDE);
  178. }
  179.  
  180. ////////////////////////////////////////////////////////////////////////
  181. //
  182. //    This routine is called when the window size has changed.
  183. //
  184. // Use: virtual private
  185.  
  186. void
  187. MyColorPatch::sizeChanged(const SbVec2s &newSize)
  188. //
  189. ////////////////////////////////////////////////////////////////////////
  190. {
  191.     // reset projection
  192.     glXMakeCurrent(getDisplay(), getNormalWindow(), getNormalContext());
  193.     glViewport(0, 0, newSize[0], newSize[1]);
  194.     glMatrixMode(GL_PROJECTION);
  195.     glLoadIdentity();
  196.     glOrtho(0, newSize[0], 0, newSize[1], -1, 1);
  197. }
  198.